package JDBC_Demo;

import java.sql.*;

public class JDBCDemo {
    public static final String URL = "jdbc:mysql://localhost:3306/blog1?useSSL=false";
    public static final String USER = "root";
    public static final String PASSWORD = "";
    public static void main(String[] args) throws Exception {
        //1.加载驱动程序
        Class.forName("com.mysql.jdbc.Driver");
        //2. 获得数据库连接
        Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
        //3.操作数据库，实现增删改查
        Statement stmt = conn.createStatement();


        try {
            conn.setAutoCommit(false);
            ResultSet rs = stmt.executeQuery("SELECT * FROM tb_category");
            //如果有数据，rs.next()返回true
            while(rs.next()){
                System.out.println(rs.getString("category_name")+" ====>"+rs.getInt("id"));
            }

            conn.commit();
        } catch (Exception e) {
            conn.rollback();
            e.printStackTrace();
        }

        stmt.close();
        conn.close();
    }

}
